package jevon.philautus.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * 将尾部带的"/"的URL过滤为不带"/"的
 * @author fujinjun
 *
 */
public class UrlRewriteInterceptor extends HandlerInterceptorAdapter {

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String path = request.getRequestURI();
		//如果URL为ContextPath，则不进行过滤
		if (path.endsWith("/")
				&& (!path.equals(request.getContextPath() + "/"))) {
			response.sendRedirect(path.substring(0, path.length() - 1));
		}
		return super.preHandle(request, response, handler);
	}

}
